# -*- coding:utf-8 -*-
"""
Created on 17 jun 2012

@author: Olof Manbo
"""

from google.appengine.api import users
from google.appengine.ext import webapp

import simplejson
import models
import logging
import gameData

class IsNewAdmin(webapp.RequestHandler):
    
    
    def get(self):
        user = users.get_current_user()

        if user:
            if users.is_current_user_admin():
                query = models.Administrator.all()
                
                isNew = True
                for admin in query.fetch(1000, 0):
                    logging.info(admin)
                    if admin.loginName == user.nickname():
                        isNew = False
                self.response.out.write(simplejson.dumps(isNew))
            else:
                self.response.out.write(simplejson.dumps(False))
        else:
            self.redirect(users.create_login_url(self.request.uri))

class CreateAdmin(webapp.RequestHandler):
    def post(self):
        user = users.get_current_user()
        logging.info(user.nickname())
        if user:
            if users.is_current_user_admin():
                query = models.Administrator.all()
                logging.info(user.nickname())
                isNew = True
                for admin in query.fetch(1000, 0):
                    if admin.loginName == user.nickname():
                        isNew = False
                if isNew:
                    newAdmin = models.Administrator()
                    newAdmin.loginName = user.nickname()
                    newAdmin.players = []
                    newAdmin.put()
                    logging.info("Oh yes")
        else:
            self.redirect(users.create_login_url(self.request.uri))

class AreaData(webapp.RequestHandler):
    def get(self):
        user = users.get_current_user()

        if user:
            logging.info(self.request.get('area'))
            if gameData.areas.has_key(self.request.get('area')):
                self.response.out.write(simplejson.dumps(gameData.areas[self.request.get('area')]))
            else:
                self.response.out.write(simplejson.dumps(False))
        else:
            self.redirect(users.create_login_url(self.request.uri))

class PlayerForm(webapp.RequestHandler):
    def post(self):
        user = users.get_current_user()
        logging.info(user.nickname())
        if user:
            if users.is_current_user_admin():
                query = models.Administrator.all()
                logging.info(user.nickname())
                currentAdmin = None
                for admin in query.fetch(1000, 0):
                    if admin.loginName == user.nickname():
                        currentAdmin = admin
                if currentAdmin is not None:
                    logging.info(self.request.get('name'))
                    currentAdmin.players.append(self.request.get('name'))
                    currentAdmin.put()
                    logging.info(currentAdmin.players)
                    newPlayer = models.Player()
                    newPlayer.loginName = user.nickname()
                    newPlayer.userName = self.request.get('name')
                    logging.info(self.request.get('name'))
                    newPlayer.put()
                    self.response.out.write(self.request.get('name'))
        else:
            self.redirect(users.create_login_url(self.request.uri))
    